#ifndef __SORFTTIME_APP_H
#define __SORFTTIME_APP_H

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdint.h>

#define ST_SORFTTIMER_VERSION "ST_RV_5.1.6"

    // #define ST_CLIENT_SERVER_MODE 1 /*取消注释切换到前后台模式*/

#define MAX_TIMER_NUM 16 /* 定时器最大个数(可修改) */

    /**移植修改步骤**/
    /*
     1：st_sorft_timer_init(); 初始化
     2: 把	st_sorft_timer_counter_prosess(); 放入1ms中断回调或1ms的OS任务中
     3: 修改 MAX_TIMER_NUM  的定义值，比如需要用到10个定时器就改为>=10
     4: 查看底部例程使用方法
     5: 记得把printf注释掉，如果没有配置printf会停在printf的while循环里面
    */

#if 1
    typedef unsigned char u8;
    typedef unsigned short int u16;
    typedef unsigned int u32;
#endif
#if 0
	typedef unsigned char uint8_t;
	typedef unsigned short int uint16_t;
	typedef unsigned int uint32_t;
#endif

    typedef enum timer_run_mode
    {
        TIM_INTERUPUT_MODE = 0, /* 中断模式*/
#ifdef ST_CLIENT_SERVER_MODE
        TIM_CLIENT_SERVER_MODE = 1, /* 前后台模式*/
#endif

    } st_e_run_mode_t;

    /***********状态枚举***********/
    typedef enum timer_init_state
    {
        TIM_CRAT_SUCES = 0, /* create succes 创建成功状态 */
        TIM_CRAT_ERROR = 1, /* create faild  创建失败状态 */

    } st_e_state_crat_t;

    typedef enum timer_run_state
    {
        TIM_LOCK = 2,   /* 锁定计数，不能再次更新计数值，只能等到计数结束 */
        TIM_UNLOCK = 3, /* 不锁定计数，可以中途手动更新计数值 */
        TIM_END = 4,    /* 计数值到达目标值，停止计数 */
    } st_e_state_run_t;

    typedef enum timer_end_state
    {
        TIM_ONCE = 5,  /* 单次计数模式*/
        TIM_CYCLE = 6, /* 循环计数模式*/
    } st_e_state_end_t;

    typedef enum timer_close_state
    {
        TIM_DESTROYED = 7, /* 销毁状态 (擦除这个定时器所占坑位资源)*/
        TIM_SUSPEND = 8    /* 挂起定时器 (会占用一个定时器坑位，不推荐)*/
    } st_e_state_close_t;

    typedef void (*st_rtn_func_t)(uint16_t);

    void st_sorft_timer_init(void);            /*初始化软件定时器*/
    void st_sorft_timer_counter_prosess(void); /*此函数放入定时器中断回调函数*/
#ifdef ST_CLIENT_SERVER_MODE
    /*这个函数放入main 的while循环*/
    void st_sorft_timer_event_prosess(void);
#endif

    st_e_state_crat_t st_start_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state); /*开启一个定时器*/

    st_e_state_crat_t st_start_once_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state); /*开启一次定时器*/

    st_e_state_crat_t st_start_cb_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state, st_e_state_end_t end_state, st_rtn_func_t rtn_func); /*计时结束,执行回调函数,函数执行完毕自动销毁该定时器*/

    st_e_state_close_t st_close_timer(uint16_t timer_id, st_e_state_close_t close_state); /* 关掉或者暂停一个定时器 */

    st_e_state_run_t st_timer_state(uint16_t timer_id); /* 读取定时器状态 */

    uint32_t st_timer_count_value(uint16_t timer_id); /*查询定时器计数值 */

    uint32_t st_get_all_runtime(void); /*获取定时器从启动到现在的总时间*/

    void st_print_time_wheel(void); /*打印定时器结构*/
    //**使用例程 start**//
    /*
    #define EVENT_ID_LED 0x10
    #define EVENT_ID_GPIO 0x11
    #define EVENT_ID_SPI 0x12
    #define EVENT_ID_TIME 0x13

    #define LOG(...) printf(__VA_ARGS__)

    void app_main_process(uint16_t id)
    {
        if (id == EVENT_ID_LED)
        {
            st_start_cb_timer(EVENT_ID_LED, 30, TIM_LOCK, TIM_ONCE, app_main_process);
            LOG("LED\n");
            return;
        }
        if (id == EVENT_ID_GPIO)
        {
            st_start_cb_timer(EVENT_ID_GPIO, 40, TIM_LOCK, TIM_ONCE, app_main_process);
            LOG("GPIO\n");
            return;
        }
        if (id == EVENT_ID_SPI)
        {
            LOG("SPI\n");
            return;
        }
    }

    main()
    {
        st_sorft_timer_init();

        st_start_cb_timer(EVENT_ID_LED, 5, TIM_LOCK, TIM_ONCE, app_main_process);
        st_start_cb_timer(EVENT_ID_GPIO, 10, TIM_LOCK, TIM_CYCLE, app_main_process);
        st_start_cb_timer(EVENT_ID_SPI, 20, TIM_UNLOK, TIM_CYCLE, app_main_process);

        while (1)
        {
            // st_sorft_timer_event_prosess(); //前后台模式
        }
        return 0;
    }

    */
    //**end**//

#ifdef __cplusplus
}
#endif
#endif
